<cfparam name="Order_ID" type="integer" default="0" />
<cfparam name="Organization_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="orders" />
<cfparam name="passback" type="string" default="" />

<cfscript>
	orderGateway = new assets.calgrants.com.gateway.OrderGateway();
	order = orderGateway.get(Order_ID = Order_ID);
	if (order.getOrder_ID() eq 0 AND request.authUser.organizationID gt 0) {
		order.setOrganization_ID(request.authUser.organizationID);
	} else if (Organization_ID gt 0) {
		order.setOrganization_ID(Organization_ID);
	}
	if (order.getOrder_ID() eq 0) {
		order.setOrdered_By_ID(GetAuthUser());
	}
</cfscript>

<cfif order.getOrganization_ID() eq 0>

	<cfset returnView = "editOrder" />
	<cfset cancelView = "orders" />
	<cfset passback = "Order_ID,0" />
	<cfinclude template="/assets/calgrants/ui/forms/selectOrganization.cfm">

<cfelseif order.getOrganization_ID() gt 0 and IsNumeric(order.getOrder_ID())>

	<cfscript>
		storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
		qNextWorkshopDate = storedProcedureGateway.call(sp = "getNextWorkshopDate", Organization_ID = order.getOrganization_ID());
		nextWorkshopDate = qNextWorkshopDate.Next_Workshop_Date;
	</cfscript>
	
	<cfif order.getOrder_ID() eq 0 AND NOT IsDate(nextWorkshopDate)>
		<!--- if no future workshops, can't order --->
		<!--- <script type="text/javascript" language="javascript">
		<!--
			$(function() {
				$("#alertModal").modal();
				resizeModal("alertModal");
			})
		//-->
		</script> --->
		
		<cfoutput>
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="span12">
						<p>You must have at least one registered workshop scheduled to place an order.</p>
						<!--- <p>Please register a workshop or contact #request.personality.name# at <a href="mailto:#request.personality.email#">#request.personality.email#</a> to place an order.</p> --->
					</div>
				</div>
				<div class="row-fluid">
					<div class="span12">
						<span class="pull-right">
							<a class="btn btn-success" href="?view=#returnView#">Close</a>
						</span>
					</div>
				</div>
			</div>
		</cfoutput>
		
	<!--- <cfelseif order.getOrder_ID() eq 0 AND Now() gt DateAdd("ww", -1, nextWorkshopDate)>
		<!--- nextWorkshopDate must be at least 4 weeks in the future, otherwise contact CCFC for late orders --->
		<cfoutput>
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="span12">
						<p>Your next scheduled workshop is on #DateFormat(nextWorkshopDate, "dddd, mmmm d, yyyy")# which is within 4 weeks of today.</p>
						<p>Orders must be placed at least 4 weeks in advance of your workshop.</p>
						<p>Please reschedule your workshop to more than 4 weeks from today or contact #request.personality.name# at <a href="mailto:#request.personality.email#">#request.personality.email#</a> to place an order.</p>
					</div>
				</div>
				<div class="row-fluid">
					<div class="span12">
						<span class="pull-right">
							<a class="btn btn-success" href="?view=#returnView#">Close</a>
						</span>
					</div>
				</div>
			</div>
		</cfoutput> --->
		
	<cfelse>
	
		<cfscript>
			organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
			organization = organizationGateway.get(Organization_ID = order.getOrganization_ID());
			userGateway = new assets.calgrants.com.gateway.UserGateway();
			orderedBy = userGateway.get(User_ID = order.getOrdered_By_ID());
			orderValueGateway = new assets.calgrants.com.gateway.OrderValueGateway();
			orderValues = orderValueGateway.query(Active = 1);
		</cfscript>

		<script type="text/javascript" language="javascript">
		<!--
			<cfoutput>
				var orderValues = #SerializeJSON(orderValues, false)#;
			</cfoutput>
			
			validate = function() {
				var errors = "";
				var valid = true;
				var qtys = $(".quantities");
				for (var i=0; i<qtys.length; i++) {
					if (isNaN(parseInt(qtys[i].value)) || parseInt(qtys[i].value) < 0) {
						valid = false;
						errors += "Every item must have a value.\n\nEnter 0 \(zero\) if you do not want to order any particular item.";
						break;
					}				
				};
				if (!valid) {
					alert(errors);
				}
				return valid;		
			}
		//-->
		</script>
		
		<cfoutput>
		
			<cfform id="orderForm" name="orderForm" action="#script_name#" method="post" onsubmit="return validate();">
				<cfinput type="hidden" name="action" value="saveOrder" />
				<cfinput type="hidden" name="view" value="#returnView#" />
				<cfinput type="hidden" name="Order_ID" value="#order.getOrder_ID()#" />
				<cfinput type="hidden" name="Organization_ID" value="#order.getOrganization_ID()#" />
				<cfinput type="hidden" name="Ordered_By_ID" value="#order.getOrdered_By_ID()#" />
				<cfinput type="hidden" name="Program_Year" value="#request.currentProgramYear#" />
				
				<div class="container-fluid">
					<div class="row-fluid">
						<div class="span4">
							<cfif IsUserInRole("Super User")>
								<div>
									<label class="required">Status</label>
									<cfselect name="Approved" required="yes" message="Status is required" class="span9">
										<option value="0"<cfif order.getApproved() eq 0> selected</cfif>>Not approved</option>
										<option value="1"<cfif order.getApproved() eq 1> selected</cfif>>Approved</option>
									</cfselect>
								</div>
							<cfelse>
								<div class="text-control">
									<label>Status</label>
									<!--- <input type="hidden" name="Approved" value="#order.getApproved()#" /> --->
									<cfif order.getOrder_ID() eq 0>
										New
									<cfelseif order.getApproved() eq 1>
										Approved
									<cfelse>
										Pending Approval
									</cfif>
								</div>
							</cfif>
							<cfif order.getOrder_ID() gt 0>
								<div class="text-control">
									<label>Order Date</label>
									#DateFormat(order.getDate_Created(), "m/d/yyyy")#
								</div>
							</cfif>
							<div class="text-control clearfix">
								<label>Next Workshop Date</label>
								<cfif IsDate(nextWorkshopDate)>
									#DateFormat(nextWorkshopDate, 'm/d/yyyy')#
								<cfelse>
									No future workshops
								</cfif>
							</div>
							<cfif order.getOrder_ID() gt 0>
								<input type="hidden" name="Anticipated_Ship_Date" value="#order.getAnticipated_Ship_Date()#" />
								<!--- <div class="text-control">
									<label>Anticipated Ship Date</label>
									#DateFormat(order.getAnticipated_Ship_Date(), "m/d/yyyy")#
								</div> --->
							<cfelse>
								<cfset anticipatedShipDate = DateAdd("ww", -4, nextWorkshopDate)>
								<input type="hidden" name="Anticipated_Ship_Date" value="#DateFormat(anticipatedShipDate, 'm/d/yyyy')#" />
								<!--- <div class="text-control">
									<label>Anticipated Ship Date</label>
									#DateFormat(anticipatedShipDate, 'm/d/yyyy')#
								</div> --->
							</cfif>
							<div class="text-control clearfix">
								<label>Approval Date</label>
								<cfif IsDate(order.getFulfillment_Date())>
									#DateFormat(order.getFulfillment_Date(), 'm/d/yyyy')#
								<cfelse>
									Not approved
								</cfif>
							</div>
							<div>
								<label class="required">Anticipated number of students</label>
								<cfinput type="text" name="Student_Count" value="#order.getStudent_Count()#" required="yes" validate="integer" min="9" message="Anticipated students is required and must be a number greater than 10" class="span3" />
							</div>
							<div>
								<label class="required">Anticipated number of volunteers</label>
								<cfinput type="text" name="Volunteer_Count" value="#order.getVolunteer_Count()#" required="yes" validate="integer" message="Anticipated volunteers is required and must be a number" class="span3" />
							</div>
						</div>
						<div class="span4">
							<div class="clearfix">
								<label>Ship To Organization</label>
								<cfinput type="text" name="Shipping_Organization" value="#order.getShipping_Organization()#" class="span9" />
							</div>
							<div class="clearfix">
								<label>Ship To Department</label>
								<cfinput type="text" name="Shipping_Department" value="#order.getShipping_Department()#" class="span9" />
							</div>
							<div class="clearfix">
								<label class="required">Ship To Recipient Name</label>
								<cfinput type="text" name="Shipping_Name" value="#order.getShipping_Name()#" required="yes" message="Ship To Recipient Name is required" class="span9" />
							</div>
							<div class="clearfix">
								<label class="required">Ship To Address</label>
								<cfinput type="text" name="Shipping_Address" value="#order.getShipping_Address()#" required="yes" message="Ship To Address is required" class="span9" />
							</div>
							<div class="clearfix">
								<label class="required">Ship To City</label>
								<cfinput type="text" name="Shipping_City" value="#order.getShipping_City()#" required="yes" message="Ship To City is required" class="span9" />
							</div>
							<div class="clearfix">
								<label class="required">Ship To Zip Code</label>
								<cfinput type="text" name="Shipping_Zip_Code" value="#order.getShipping_Zip_Code()#" required="yes" validate="zipcode" message="Ship To Zip Code is required and must be a valid zip code" class="span9" />
							</div>
							<div class="clearfix">
								<label class="required">Recipient Phone</label>
								<cfinput type="text" name="Shipping_Phone" value="#order.getShipping_Phone()#" required="yes" validate="telephone" message="Recipient Phone is required and must be a valid telephone number" class="span9" />
							</div>
						</div>
						<div class="span4">
							<div class="text-control clearfix">
								<label>Organization</label>
								#organization.getOrganization()#<br />
								#organization.getAddress()#<br />
								#organization.getCity()#, CA #organization.getZip_Code()#
							</div>
							<cfif order.getOrder_ID() gt 0>
								<div class="text-control">
									<label>Ordered By</label>
									#orderedBy.getFirst_Name()# #orderedBy.getLast_Name()#<br />
									<a href="mailto:#orderedBy.getEmail()#">#orderedBy.getEmail()#</a><br />
									#orderedBy.getPhone()#<cfif Len(Trim(orderedBy.getExtension())) gt 0> x #orderedBy.getExtension()#</cfif>
								</div>
								<div class="text-control">
									<label>Order ID</label>
									#order.getOrder_ID()#
								</div>
								<div class="text-control">
									<label>Date Created</label>
									#DateFormat(order.getDate_Created(), "mmmm d yyyy")# @ #TimeFormat(order.getDate_Created(), "h:mmtt")#
								</div>
							</cfif>
							<div class="clearfix">
								<label>Shipping Instructions</label>
								<textarea name="Shipping_Instructions" class="span9">#order.getShipping_Instructions()#</textarea>
							</div>
						</div>
					</div>
					<div class="row-fluid">
						<div class="span12">
							<table class="table table-striped table-bordered" style="margin-top: 30px;">
								<thead>
									<tr>
										<th nowrap="nowrap">Item No.</th>
										<th>Category</th>
										<th>Item</th>
										<th><label class="required">Quantity</label></th>
									</tr>
								</thead>
								<tbody>
									<cfset warnUnsync = false />
									<cfloop query="orderValues">
										<cfif ListLen(order.getOrder_Value_IDs()) eq ListLen(order.getQuantities())>
											<cfset itemQty = 0>
											<cfset itemIndex = ListFind(order.getOrder_Value_IDs(), Order_Value_ID)>
											<cfif itemIndex gt 0>
												<cfset itemQty = ListGetAt(order.getQuantities(), itemIndex)>
											</cfif>
										<cfelse>
											<cfset warnUnsync = true />
											<cfset itemQty = 0 />
										</cfif>
										<input type="hidden" name="Order_Value_IDs" value="#Order_Value_ID#" />
										<tr>
											<td>#SKU#</td>
											<td>#Category#</td>
											<td>
												<cfif Len(Trim(Image)) gt 0>
													<a href="common/images/order-images/#Image#" target="_blank">#Description#</a>
												<cfelse>
													#Description#
												</cfif>
											</td>
											<td><cfinput type="text" class="quantities" name="Quantities" value="#itemQty#" style="width: 50px;"  /></td><!--- required="yes" validate="integer" message="A quantity is required for #Replace(Replace(ListFirst(Description, '<'), '&', '\&', 'all'), '"', '\"', 'all')#" --->
										</tr>
									</cfloop>
								</tbody>
							</table>
						</div>
					</div>
					<div class="row-fluid">
						<div class="span12">
							<span class="pull-right">
								<a href="?view=#returnView#" class="btn btn-success">Cancel</a>
								<!--- <cfif order.getOrder_ID() gt 0>
									<a href="?view=#returnView#" class="btn btn-success">Print</a>
								</cfif> --->
								<!--- <a class="btn btn-success" href="?action=deleteOrder&view=#returnView#&orderID=#orderID#">Delete</a> --->
								<cfinput type="submit" name="saveButton" class="btn btn-success" value="Save" />
							</span>
						</div>
					</div>
				</div>

			</cfform>
		
		</cfoutput>
		
		<cfif warnUnsync>
			<script type="text/javascript">
				alert("Your order is corrupted.  We are sorry but certain orders had submission problems and became corrupted.\n\nWe have set all item quantities to 0 \(zero\) and you must enter the quantities again.  You will not have to do this again.\n\nSorry for the inconvenience and thank you for participating in the Cash for College program.")
			</script>
		</cfif>
		
	</cfif>

<cfelse>

	<div class="alert alert-info notFound">Order not found</div>

</cfif>